package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.DevelopmentTop;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PopularProcess;
import com.ruoyi.system.service.IPopularProcessService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 小区人气和过程管控Controller
 * 
 * @author ruoyi
 * @date 2025-08-05
 */
@RestController
@RequestMapping("/system/process")
public class PopularProcessController extends BaseController
{
    @Autowired
    private IPopularProcessService popularProcessService;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询小区人气和过程管控列表
     */
    @PreAuthorize("@ss.hasPermi('system:process:list')")
    @GetMapping("/list")
    public TableDataInfo list(PopularProcess popularProcess)
    {
        startPage();
//        List<PopularProcess> list = popularProcessService.selectPopularProcessList(popularProcess);
        List<PopularProcess> list = popularProcessService.selectPopularProcessListNew(popularProcess);
        return getDataTable(list);
    }

    /**
     * 导出小区人气和过程管控列表
     */
    @PreAuthorize("@ss.hasPermi('system:process:export')")
    @Log(title = "小区人气和过程管控", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PopularProcess popularProcess)
    {
//        List<PopularProcess> list = popularProcessService.selectPopularProcessList(popularProcess);
        List<PopularProcess> list = popularProcessService.selectPopularProcessListNew(popularProcess);
        ExcelUtil<PopularProcess> util = new ExcelUtil<PopularProcess>(PopularProcess.class);
        util.exportExcel(response, list, "小区人气和过程管控数据");
    }

    /**
     * 获取小区人气和过程管控详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:process:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(popularProcessService.selectPopularProcessById(id));
    }

    /**
     * 新增小区人气和过程管控
     */
    @PreAuthorize("@ss.hasPermi('system:process:add')")
    @Log(title = "小区人气和过程管控", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PopularProcess popularProcess)
    {
        SysDept sysDepts = sysDeptMapper.selectDeptOnName(popularProcess.getDeptName());
        popularProcess.setDeptId(sysDepts.getDeptId());
        return toAjax(popularProcessService.insertPopularProcess(popularProcess));
    }

    /**
     * 修改小区人气和过程管控
     */
    @PreAuthorize("@ss.hasPermi('system:process:edit')")
    @Log(title = "小区人气和过程管控", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PopularProcess popularProcess)
    {
        return toAjax(popularProcessService.updatePopularProcess(popularProcess));
    }

    /**
     * 删除小区人气和过程管控
     */
    @PreAuthorize("@ss.hasPermi('system:process:remove')")
    @Log(title = "小区人气和过程管控", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(popularProcessService.deletePopularProcessByIds(ids));
    }

    @Log(title = "小区核劵量导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<PopularProcess> util = new ExcelUtil<PopularProcess>(PopularProcess.class);
        List<PopularProcess> popularProcessList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = popularProcessService.importPopularProcess(popularProcessList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<PopularProcess> util = new ExcelUtil<PopularProcess>(PopularProcess.class);
        util.importTemplateExcel(response, "小区核劵量");
    }
}
